<?php

namespace app\account\controller\api;

use app\account\service\Account;

use EasyWeChat\OfficialAccount\Application;
use think\admin\Controller;
use EasyWeChat\Factory;
class Easy extends Controller
{
    /**
     * 通道认证类型
     * @var string
     */
    const type = Account::WECHAT;
    /**
     * 微信调度器
     * @var Application
     */
    private $wechat;

    /**
     * 控制器初始化
     * @return $this
     */
    protected function initialize(): Easy
    {
        $config = config('easywechat.OfficialAccount');
        if ($this->field = Account::field(static::type)) {
            $this->wechat = Factory::officialAccount($config);
            $this->target = input('source') ?: $this->request->server('http_referer', $this->request->url(true));
        } else {
            $this->error(sprintf('接口通道 [%s] 未开通！', static::type));
        }
        return $this;
    }

    public function serve() {
//        var_dump($this->wechat->base->getValidIps());
        $response = $this->wechat->server->serve();
        $response->send();

        exit;
    }

    public function jssdk() {
        $config = $this->wechat->jssdk->buildConfig([]);
//        $redirectUrl = $this->wechat->oauth->scopes(['snsapi_userinfo'])
//            ->redirect('abc');
//        var_dump($redirectUrl);
        return $config;
    }

    public function oauth() {
        $redirect_url = input('redirect_url');
        $url = $this->wechat->oauth->scopes(['snsapi_base'])->redirect($redirect_url);
        $this->success('success', $url);
    }

}